package com.springapp.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.springapp.jsonmodel.ItemInOrderJson;
import com.springapp.jsonmodel.OrderJson;
import com.springapp.model.Desk;
import com.springapp.model.ItemInOrder;
import com.springapp.model.Order;
import com.springapp.model.OrderStatus;
import com.springapp.model.Status;
import com.springapp.service.impl.ItemInOrderServiceImpl;
import com.springapp.service.impl.ItemServiceImpl;
import com.springapp.service.impl.OrderServiceImpl;
import com.springapp.service.impl.TableServiceImpl;
import java.sql.Timestamp;
import com.springapp.util.ControllerUtil;

@Controller
@RequestMapping("/")
public class OrderController {

    private final Logger log = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private OrderServiceImpl osi;
    @Autowired
    private TableServiceImpl tsi;
    @Autowired
    private ItemServiceImpl isi;
    @Autowired
    private ItemInOrderServiceImpl iiosi;

    @RequestMapping(value = "/order", method = RequestMethod.GET)
    public @ResponseBody
    String getAllOrders() {
        List<Order> orders = osi.getAll();
        List<OrderJson> json = new ArrayList<>();

        for (int i = 0; i < orders.size(); i++) {
            json.add(OrderJson.toModel(orders.get(i)));
        }

        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        String orderJson = gson.toJson(json);
        return ControllerUtil.printSuccess(orderJson);
    }

    /*
     * @author Lera Krivezhenko
     * @uthor Sim4ik
     * 
     */
    @RequestMapping(value = "/order", method = RequestMethod.POST,
    consumes = "application/json")
    public @ResponseBody
    String createNewOrder(@RequestBody String json) {
    	Gson gson = new Gson();
        OrderJson orderJson = gson.fromJson(json, OrderJson.class);
        Order order = OrderJson.fromModel(orderJson);
        Desk desk = tsi.getById(orderJson.getDeskId());
        osi.createNewOrder(order, desk);
        return ControllerUtil.printSuccess(json);
    }

    /*
     * 
     * @uthor Sim4ik
     * 
     */
    @RequestMapping(value = "/order/{id}", method = RequestMethod.GET)
    public @ResponseBody
    String getOrder(@PathVariable Integer id) {
        Order order = osi.getById(id);
        
        if (order == null) {
            return ControllerUtil.printError();
        }
        
        OrderJson json = OrderJson.toModel(order);
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        String orderJson = gson.toJson(json);
        
        return ControllerUtil.printSuccess(orderJson);
    }

    /**
     * 
     * @uthor Sim4ik
     * @author Lera Krivezhenko
     */
  /*  
    @RequestMapping(value = "/order/{id}", method = RequestMethod.PUT)
    public @ResponseBody
    String updateOrder(@RequestBody String json, @PathVariable int id) {
        Gson gson = new Gson();
        OrderJson orderJson = gson.fromJson(json, OrderJson.class);
        Order order = osi.getById(id);
        
        if (order == null) {
            return ControllerUtil.printError();
        }
        order.setDesk(tsi.getById(orderJson.getDeskId()));
        if (orderJson.getStatus() == OrderStatus.CLOSED) {
            Desk desk = tsi.getById(orderJson.getDeskId());
            desk.setStatus(Status.FREE);
            desk.setOrder(null);
            tsi.update(desk);
        }
        order.setStatus(orderJson.getStatus());
        List<ItemInOrderJson> itemInOrderJson = orderJson.getItems();
        List<ItemInOrder> itemInOrder = new ArrayList<>();
        
        
         //iiosi.deleteByOrder(order.getId());
        //iiosi.deleteByOrder(order.getId());
        List<ItemInOrder> itemInOrder_1 = iiosi.getAll();
        for (int i = 0; i < itemInOrder_1.size(); i++){
            if (itemInOrder_1.get(i).getOrder().getId() != id) {
                iiosi.delete(itemInOrder_1.get(i).getId());
            }   
        }
        for (int i = 0; i < itemInOrderJson.size(); i++) {
            ItemInOrder itemOrder = new ItemInOrder();
            itemOrder.setOrder(order);
            itemOrder.setItem(isi.getById(itemInOrderJson.get(i).getId()));
            itemOrder.setQuantity(itemInOrderJson.get(i).getCount());
            itemInOrder.add(itemOrder);
        }
        order.setItemsInOrder(itemInOrder);
        osi.update(order);
        return ControllerUtil.printSuccess(json);
    }*/

    
   @RequestMapping(value = "/order/{id}", method = RequestMethod.PUT)
    public @ResponseBody
    String updateOrder111(@RequestBody String json, @PathVariable int id) {
	   Order orderRef = osi.getById(id);
       if (orderRef == null) {
           return ControllerUtil.printError();
       }
	   
	   Gson gson = new Gson();
       OrderJson orderJson = gson.fromJson(json, OrderJson.class);
       Order order = OrderJson.fromModel(orderJson);
       //order.setId(id);
       //Desk desk = tsi.getById(orderJson.getDeskId());
       osi.updateOrder(orderRef, order);
	   
        /*Gson gson = new Gson();
        OrderJson orderJson = gson.fromJson(json, OrderJson.class);
        Order order = orderService.getById(id);*/
        
      /*  if (order == null) {
            return ControllerUtil.printError();
        }*/
        //order.setDesk(tsi.getById(orderJson.getDeskId()));
      /*  if (orderJson.getStatus() == OrderStatus.CLOSED) {
            Desk desk = tsi.getById(orderJson.getDeskId());
            desk.setStatus(Status.FREE);
            desk.setOrder(null);
            tsi.update(desk);
        }
        
        Order order_new = OrderJson.fromModel(orderJson);
        
        iiosi.deleteByOrder(order.getId());
        
        List<ItemInOrderJson> itemInOrderJson = orderJson.getItems();
        List<ItemInOrder> itemInOrder = new ArrayList<>();
        
        
        
        for (int i = 0; i < itemInOrderJson.size(); i++) {
            ItemInOrder itemOrder = new ItemInOrder();
            itemOrder.setOrder(order);
            itemOrder.setItem(isi.getById(itemInOrderJson.get(i).getId()));
            itemOrder.setQuantity(itemInOrderJson.get(i).getCount());
            itemInOrder.add(itemOrder);
        }
        order.setItemsInOrder(itemInOrder);
        osi.update(order);*/
        return ControllerUtil.printSuccess(json);
    }
    
    
    /*
     * 
     * @uthor Sim4ik
     * 
     */
    @RequestMapping(value = "/order/{id}", method = RequestMethod.DELETE)
    public @ResponseBody
    String deleteOrder(@PathVariable Integer id) {
        Order order = osi.getById(id);
        if (order == null) {
            return ControllerUtil.printError();
        }
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        OrderJson orderjson = OrderJson.toModel(order);
        String json = gson.toJson(orderjson);
        osi.delete(id);
        return ControllerUtil.printSuccess(json);
    }
}
